package com.android.test2;

/**
 * SSUI Project
 */


import android.app.Activity;
import android.database.Cursor;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.SimpleCursorAdapter;
import android.widget.Spinner;
import android.widget.TextView;


public class AndroidTest2 extends Activity{
	
	//We need to obtain/update information from these Widgets:  
	Button button;
	//Source and Target currencies
	Spinner spinnerSource;
    Spinner spinnerTarget;
    //Amount to convert
    EditText amount;
    //Widget to display the answer
    TextView value;
	
	
	private DbAdapter theadapter;
	
	//private static final String[] currencies = {"USD","Euro","British Pound","Taiwanese New Dollar"};
    private static final double[] values = {0.704179,1,1.15395,0.0238060};
	
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //We set the root layout 
        setContentView(R.layout.main);
        
        //Get widgets from xml file in order to use them
        getWidgets();
        
    	
    }
    
    public void getWidgets(){
    	/* @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
    	 * Fill the information of the spinners.
    	  @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ */
        
        /*DEPRECATED :)*/ //dataSpinnerFromXML();
    	//dataSpinnerFromArrays();
    	dataSpinnetFromDB();
        
        button = (Button) this.findViewById(R.id.converter);
        button.setOnClickListener(myListener);
        
        amount = (EditText) this.findViewById(R.id.amount);
        value = (TextView) this.findViewById(R.id.value);
    }
    
    public void dataSpinnetFromDB(){
    	
    	//Create an object of the class Adapter, where is the database
        theadapter = new DbAdapter(this);
    	theadapter.open();
    	
    	
    	Cursor thecursor = theadapter.fetchAllCurrencies();
        startManagingCursor(thecursor);
        
    	spinnerSource = (Spinner) findViewById(R.id.soucerS);
        spinnerTarget = (Spinner) findViewById(R.id.targetS);
        SimpleCursorAdapter adapter = new SimpleCursorAdapter(this,
        	    android.R.layout.simple_spinner_item, // Use a template
        	                                          // that displays a
        	                                          // text view
        	    thecursor, // Give the cursor to the list adapter
        	    new String[] {"currency"}, // Map the NAME column in the
        	                                         // people database to...
        	    new int[] {android.R.id.text1}); // The "text1" view defined in
        	                                     // the XML template
        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
         
         //Set the values to each spinner. Data from xml file.
         spinnerSource.setAdapter(adapter);
         spinnerTarget.setAdapter(adapter);
    }
    
    /*public void dataSpinnerFromArrays(){
    	spinnerSource = (Spinner) findViewById(R.id.soucerS);
        spinnerTarget = (Spinner) findViewById(R.id.targetS);
        
        ArrayAdapter<String> adapter = new ArrayAdapter<String>(
               this, android.R.layout.simple_spinner_item,currencies);
       adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        
        //Set the values to each spinner. Data from xml file.
        spinnerSource.setAdapter(adapter);
        spinnerTarget.setAdapter(adapter);
        
    }*/
    
    //This method is deprecated. This is just to see the way of use
    //xml file instead arrays
    public void dataSpinnerFromXML(){
    	spinnerSource = (Spinner) findViewById(R.id.soucerS);
        spinnerTarget = (Spinner) findViewById(R.id.targetS);

        //Create an adapter. The values currencies.
        ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(
               this, R.array.values_array, android.R.layout.simple_spinner_item);
        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        
      //Set the values to each spinner. Data from xml file.
        spinnerSource.setAdapter(adapter);
        spinnerTarget.setAdapter(adapter);
    }
    
    private OnClickListener myListener = new OnClickListener() {
    	public void onClick(View v){
    		int vsource = spinnerSource.getSelectedItemPosition();
    		int vtarget = spinnerTarget.getSelectedItemPosition();
    		double vamount = Double.parseDouble(amount.getText().toString());
        	value.setText(Double.toString(converter(vsource,vtarget,vamount)));
        }
    };
  
    public double converter(int source, int target, double amount ){
    	return (amount * values[source])/values[target];
    }
    
}